﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "select * from profile where cookname=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("firstname")) Then txtfirstname.Text = dr("firstname")
                If Not IsDBNull(dr("lastname")) Then txtfirstname.Text = dr("lastname")
                If Not IsDBNull(dr("emailaddress")) Then txtemailaddress.Text = dr("emailaddress")
                
            End If

        End If
      
    End Sub

    Protected Sub cmdsave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdsave.Click

        Dim newfilename As String = ""
        Dim SqlString As String
        If picsupload.HasFile Then
            Dim myguid = Guid.NewGuid()
            newfilename = myguid.ToString() & ".jpg"
            Dim filelocationonserverhardisk = Request.MapPath("pictures") & "/" & newfilename
            picsupload.SaveAs(filelocationonserverhardisk)
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        If picsupload.HasFile Then
            SqlString = "UPDATE profile SET firstname=@f1, lastname=@f2, emailaddress=@f3, phonenumber=@f4, picture=@f5 where cookname=@f6"
        Else
            SqlString = "UPDATE profile SET firstname=@f1, lastname=@f2, emailaddress=@f3, phonenumber=@f4 where cookname=@f6"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", txtfirstname.Text)
        cmd.Parameters.AddWithValue("@f2", txtlastname.Text)
        cmd.Parameters.AddWithValue("@f3", txtemailaddress.Text)
        cmd.Parameters.AddWithValue("@f4", txtphonenumber.Text)
        If picsupload.HasFile Then cmd.Parameters.AddWithValue("@f5", "~/pictures/" & newfilename)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("~/profile.aspx?cookname=" & User.Identity.Name)
    End Sub
End Class